package com.ysh.smartplatform.service.impl;

import com.ysh.smartplatform.cache.LoginLogCache;
import com.ysh.smartplatform.event.LoginLogChangeEvent;
import com.ysh.smartplatform.exceptions.FindDataException;
import com.ysh.smartplatform.mapper.LoginLogMapper;
import com.ysh.smartplatform.pojo.SysCategory;
import com.ysh.smartplatform.pojo.SysDevice;
import com.ysh.smartplatform.pojo.SysLogininfor;
import com.ysh.smartplatform.service.LoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 袁胜虎 on 2022/9/6 11:47
 */
@Service
@Transactional
public class LoginLogServiceImpl implements LoginLogService {
    private ApplicationContext applicationContext;

    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    private LoginLogCache loginLogCache;

    @Autowired
    public void setLoginLogCache(LoginLogCache loginLogCache) {
        this.loginLogCache = loginLogCache;
    }

    private LoginLogMapper loginLogMapper;

    @Autowired
    public void setLoginLogMapper(LoginLogMapper loginLogMapper) {
        this.loginLogMapper = loginLogMapper;
    }

    @Override
    public void addLog(SysLogininfor sysLogininfor) {
        loginLogMapper.addLog(sysLogininfor);
    }

    @Override
    public List<SysLogininfor> findByName(String loginName) {

        List<SysLogininfor> byName = loginLogMapper.findByName(loginName);

        return byName;
    }
    @Override
    public void deleteById(Long id) {
        loginLogMapper.deleteById(id);
        applicationContext.publishEvent(new LoginLogChangeEvent());
    }

    @Override
    public List<SysLogininfor> findAllSysLoginInFor() {
        List<SysLogininfor> sysLoginInForList = loginLogCache.getSysLoginInForList();
        return sysLoginInForList.stream().filter(sysLogininfor-> sysLogininfor.getStatus() == 1).collect(Collectors.toList());
    }

    @Override
    public SysLogininfor findByLoginName(String loginName) {
        SysLogininfor loginInFor = loginLogCache.get(loginName);
        System.err.println(loginName);
        System.err.println(loginInFor);
        return loginInFor;
    }
}
